﻿// JScript 文件
// 事件功能集
//最成功的一次鼠标拖动效果
var EventOperate=new Object();

//添加事件
EventOperate.addEvent=function(obj,handle,func){
    if(EventOperate.____addedEventObj.indexOf(obj)==-1)
        EventOperate.____addedEventObj.push(obj);
    
    if(!obj.e___events) obj.e___events={};
    if(!obj.e___events[handle]) obj.e___events[handle]={funs:[],opts:[]};
    if(obj.e___events[handle].funs.indexOf(func)==-1){
        function opt(e){
            return func(e,obj);
        }
        obj.e___events[handle].funs.push(func);
        obj.e___events[handle].opts.push(opt);
        if(obj.attachEvent){
            obj.attachEvent("on"+handle,opt);
        }else if(obj.addEventListener){
            obj.addEventListener(handle,opt,false);
        }else{
            obj["on"+handle]=opt;
        }
    }
}

//添加一组事件
EventOperate.addEvents=function(obj,handleList){
    for(var handle in handleList){
        EventOperate.addEvent(obj,handle,handleList[handle]);
    }
}

//清除事件
EventOperate.delEvent=function(obj,handle,func){
    if(!obj.e___events) return;
    if(!obj.e___events[handle]) return;
    var n=obj.e___events[handle].funs.indexOf(func);    
    if(n!=-1){
        var opt=obj.e___events[handle].opts[n];
        if(obj.detachEvent){
            obj.detachEvent("on"+handle,opt);
        }else if(obj.removeEventListener){
            obj.removeEventListener(handle,opt,true);
        }else{
            obj["on"+handle]=null;
        }
        obj.e___events[handle].funs.removeAt(n);
        obj.e___events[handle].opts.removeAt(n);
    }
}

//清除某个部件的所有事件
EventOperate.clearEvents=function(obj){
    if(!obj.e___events) return;
    for(var e in obj.e___events){
        var opts=obj.e___events[e];
        for(var o in opts){
            EventOperate.delEvent(obj,e,o);
        }
    }
}

//事件代理,即使能事件处理fun能找到正确的this
//obj的值统一为this
//fun为事件处事程序,它要实现两个参数,(obj,info)事件发生者,传入信息
EventOperate.createDelegate=function(obj,fun){
    function opt(e,o){
        if(!e) e=event;
        if(o) var oThis=o;
        else var oThis=this;//注,这个this为事件的发生者
        var r= fun.call(obj,oThis,e);
        if(r==false){
            if(e.preventDefault) e.preventDefault();
        }
        return r;
    }
    return opt;
}


//当鼠标点下时添加可拖动效果
EventOperate.addMoveObj=function(obj,event){
    if(!document.getScrollLeft){
        alert("请加入DocumentSize.js");
        return;
    }
    var objX=EventOperate.getObjX(obj);
    var objY=EventOperate.getObjY(obj);
    var mouseX=event.clientX+document.getScrollLeft();
    var mouseY=event.clientY+document.getScrollTop();
    function objMove(event){
        var newMouseX=event.clientX+document.getScrollLeft();
        var newMouseY=event.clientY+document.getScrollTop();
        obj.style.left=(newMouseX-mouseX+objX)+"px";
        obj.style.top=(newMouseY-mouseY+objY)+"px";
    }
    function delMove(event){
        EventOperate.delEvent(document,"mousemove",objMove);
    }

    EventOperate.addEvent(document,"mousemove",objMove);
    EventOperate.addEvent(document,"mouseup",delMove);
}

//取得鼠标击活的对象
EventOperate.getTarget=function(event){
    if(event.srcElement){
        return event.srcElement;
    }else if(event.target){
        return event.target;
    }
}

//取消冒泡事件
EventOperate.cancelBubble=function(event){
    if (event.stopPropagation) event.stopPropagation();
    event.cancelBubble = true;
    return false;
}

//左键，选择取消
EventOperate.FixedPart =function(obj){
    obj.onselectstart=function(){return false;}
    obj.oncontextmenu=function(){return false;}
    obj.style.MozUserSelect="none";
}

EventOperate.getObjX=function(obj){
    var left=obj.offsetLeft;
    while(obj=obj.offsetParent){
        left+=obj.offsetLeft;
    }
    return left;
}

EventOperate.getObjY=function(obj){
    var top=obj.offsetTop;
    while(obj=obj.offsetParent){
        top+=obj.offsetTop;
    }
    return top;
}

//------------------------------------------------------------------------------
//此为一套处理集合
//添加某个处理事件到集合中
//obj为区分对象
//key为区分字符串
//hander为处理方法,hander有两个参数(Object,info)事件传递者,消息
EventOperate.addHandler=function(obj,key,hander){
    if(!obj.h__handlerGroup) obj.h__handlerGroup={};
    if(!obj.h__handlerGroup[key]) obj.h__handlerGroup[key]=[];
    var opts=obj.h__handlerGroup[key];
    if(opts.indexOf(hander)==-1) obj.h__handlerGroup[key].push(hander);
}

//删除事件处理
//参数同上
EventOperate.removeHandler=function(obj,key,hander){
    if(!obj.h__handlerGroup) return;
    var opts=obj.h__handlerGroup[key];
    if(!obj) return;
    opts.remove(hander);
}

//取出处理集合
//key事件关键字
EventOperate.getHandler=function(obj,key){
    if(!obj.h__handlerGroup) return null;
    var opts=obj.h__handlerGroup[key];
    if(!opts) return null;
    function opt(sendObj,info){
        var returnValue=true;
        for(var i=0;i<opts.length;i++){
            var v=opts[i](sendObj,info);
            if(v&&v==false) returnValue=false;
        }
        return returnValue;
    }
    return opt;
}

//使addHandler方法添加的事件能正确找到this
//obj为this,fun为处理方法
Function.createDelegate=function(obj,fun){
    function opt(sender,e){
        var oThis=this;//注,这个this为事件的发生者
        return fun.call(obj,sender,e);
    }
    return opt;
}

//在类中产生一个注册，obj一般为this
Function.registerClass=function(obj){
    Function.____rememberObjs.push(obj);
}

//回收
Function.recycleClass=function(obj){
    Function.____rememberObjs.remove(obj);
    delete obj;obj=null;
}

//延时器
//可用Function.createDelegate找到this
Function.Timelag=function(func,space){
    Function.registerClass(this);
    this._func=func;
    this._space=space;
    this._sto=null;
}
Function.Timelag.prototype.run=function(){
    this._sto=setTimeout(this._func,this._space)
}
Function.Timelag.prototype.cancel=function(){
    if(this._sto) clearTimeout(this._sto);
}
Function.Timelag.prototype.dispose=function(){
    if(this._sto) clearTimeout(this._sto);
    this._func=null;
    this._space=null;
    this._sto=null;
}

//------------------------------------------------------------------------------

//内部操作，不对外开放。
EventOperate.___removeAllEvent=function(){
    //注消事件
    var len=EventOperate.____addedEventObj.length;
    for(var i=0;i<len;i++){
        var obj=EventOperate.____addedEventObj[i];
        EventOperate.clearEvents(obj);
        delete obj;obj=null;
        EventOperate.____addedEventObj[i]=null;
    }
    delete EventOperate.____addedEventObj;
    EventOperate.____addedEventObj=null;
    
    //销毁对象
    len=Function.____rememberObjs.length;
    for(var i=0;i<len;i++){
        var _obj=Function.____rememberObjs[i];
        if(_obj&&_obj.dispose) _obj.dispose();
        if(Function.____rememberObjs.indexOf(_obj)!=-1) Function.recycleClass(_obj);
    }
    delete Function.____rememberObjs;
    Function.____rememberObjs=null;
    delete EventOperate;EventOperate=null;
    if(document.all) CollectGarbage(); 
    return false;
}

EventOperate.____addedEventObj=[];
Function.____rememberObjs=[];

EventOperate.addEvent(document,"unload",EventOperate.___removeAllEvent);